import { math, EventTarget, _decorator } from "cc"
const { ccclass, property } = _decorator;
import { PlayerPreference } from "./PlayerPreference"

@ccclass('Setting')
export class Setting extends EventTarget {

    private static _instance: Setting = null

    public static get instance(): Setting {
        if (this._instance === null) {
            this._instance = new Setting()
        }
        return this._instance
    }


    // bgm
    private _bgmVolume: number = 0.8
    
    public get bgmVolume() : number {
        return this._bgmVolume
    }

    public set bgmVolume(value: number) {
        this._bgmVolume = math.clamp01(value)
        PlayerPreference.setFloat('bgmVolume', this._bgmVolume)
        this.emit('onBgmVolumeChanged', this._bgmVolume)
    }

    // sfx
    private _sfxVolume: number = 0.8

    public get sfxVolume() : number {
        return this._sfxVolume
    }

    public set sfxVolume(value: number) {
        this._sfxVolume = math.clamp01(value)
        PlayerPreference.setFloat('sfxVolume', this._sfxVolume)
    }

    public load() {
        this._bgmVolume = PlayerPreference.getFloat('bgmVolume')
        if (isNaN(this._bgmVolume)) this._bgmVolume = 0.8
        this._sfxVolume = PlayerPreference.getFloat('sfxVolume')
        if (isNaN(this._sfxVolume)) this._bgmVolume = 0.8
    }
    
    
}